<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <el-form ref="queryForm" :model="listQuery.param" size="small" :inline="true" label-width="auto">
        <el-form-item label="发送账号" prop="account">
          <el-input v-model="listQuery.param.account" placeholder="发送账号" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="推送状态" prop="pushStatus">
          <el-select v-model="listQuery.param.pushStatus" placeholder="推送状态" class="filter-item" @change="handleFilterQuery">
            <el-option v-for="item in pushStatusOptions" :key="item.code" :label="item.text" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="下发状态" prop="sendStatus">
          <el-select v-model="listQuery.param.sendStatus" placeholder="下发状态" class="filter-item" @change="handleFilterQuery">
            <el-option v-for="item in pushStatusOptions" :key="item.code" :label="item.text" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </el-form>
    </div>
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />
      <el-table-column label="请求流水号" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.requestNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属应用" min-width="90px" align="left">
        <template v-slot="{row}">
          <span>{{ row.serviceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息标题" min-width="120px" align="left">
        <template v-slot="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送账号" min-width="120px" align="left">
        <template v-slot="{row}">
          <span>{{ row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送状态" min-width="60px" align="left">
        <template v-slot="{row}">
          <span v-if="row.pushStatus === 'S'">成功</span>
          <span v-else-if="row.pushStatus === 'F'">失败</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="发送方式" min-width="60px" align="left">
        <template v-slot="{row}">
          <span v-if="row.mode === 'SMS'">短信</span>
          <span v-else-if="row.mode === 'EMAIL'">邮件</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="下发状态" min-width="70px" align="left">
        <template v-slot="{row}">
          <span v-if="row.sendStatus === 'S'">成功</span>
          <span v-else-if="row.sendStatus === 'F'">失败</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="租户ID" min-width="80px" align="left">
        <template v-slot="{row}">
          <span>{{ row.tenantId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" min-width="70px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="90px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <el-button size="mini" type="info" icon="el-icon-document" @click="handleDetail(row, $index)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <!-- 编辑页面 -->
    <el-drawer
      :visible.sync="visible"
      direction="rtl"
      size="50%"
      :close-on-press-escape="false"
      :wrapper-closable="false"
      :show-close="false"
    >
      <template v-slot:title>
        <div class="drawer-header">
          <span>{{ title[state] }}</span>
          <div>
            <el-button icon="el-icon-close" @click="handleClose">
              取消
            </el-button>
            <el-button v-if="state !=='detail'" type="primary" icon="el-icon-check">
              确认
            </el-button>
          </div>
        </div>
      </template>
      <el-form ref="dataForm" :rules="rules" :model="formData" label-width="100px">
        <el-input v-model="formData.id" type="hidden" />
        <!--按需调整-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="请求流水号" prop="requestNo">
              <el-input v-model="formData.requestNo" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属应用名称" prop="serviceName">
              <el-input v-model="formData.serviceName" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="消息标题" prop="title">
              <el-input v-model="formData.title" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送账号" prop="account">
              <el-input v-model="formData.account" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="发送内容" prop="content">
              <el-input v-model="formData.content" :readonly="state==='detail'" :autosize="{ minRows: 10, maxRows: 25}" type="textarea" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="推送状态" prop="pushStatus">
              <el-radio v-model="formData.pushStatus" :disabled="state==='detail'" label="S">成功</el-radio>
              <el-radio v-model="formData.pushStatus" :disabled="state==='detail'" label="F">失败</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送状态" prop="sendStatus">
              <el-radio v-model="formData.pushStatus" :disabled="state==='detail'" label="S">成功</el-radio>
              <el-radio v-model="formData.pushStatus" :disabled="state==='detail'" label="F">失败</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送方式" prop="mode">
              <el-select v-model="formData.mode" :disabled="state==='detail'" style="width: 100%;" placeholder="请选择" clearable>
                <el-option key="SMS" label="短信" value="SMS" />
                <el-option key="EMAIL" label="邮件" value="EMAIL" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消息类型" prop="type">
              <el-input v-model="formData.type" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="厂商名称" prop="vendorName">
              <el-input v-model="formData.vendorName" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下发流水号" prop="serialNo">
              <el-input v-model="formData.serialNo" :readonly="state==='detail'" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" :readonly="state==='detail'" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="handleClose">
          取消
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getPageList } from './api'
import permission from '@/directive/permission' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination/index.vue' // secondary package based on el-pagination

export default {
  // 当前页面名称（组件）
  name: 'MessageInfo',
  // 引用组件
  components: { Pagination },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          account: undefined,
          pushStatus: undefined,
          sendStatus: undefined
        }
      },
      // 状态字典
      statusOptions: [{ code: 'Y', text: '启用' }, { code: 'N', text: '禁用' }],
      pushStatusOptions: [{ code: 'S', text: '成功' }, { code: 'F', text: '失败' }],
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {
        update: '修改',
        create: '新增',
        detail: '详情'
      },
      // 表单校验规则
      rules: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
    this.handleRestFormData()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    getList() {
      this.loading = true
      getPageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.$refs.queryForm.resetFields()
    },
    // 重置表单
    handleRestFormData() {
      // 按需调整
      this.formData = {
        id: undefined,
        requestNo: undefined,
        serviceName: undefined,
        serviceCode: undefined,
        title: undefined,
        account: undefined,
        content: undefined,
        pushStatus: undefined,
        sendStatus: undefined,
        mode: undefined,
        type: undefined,
        vendorCode: undefined,
        vendorName: undefined,
        serialNo: undefined,
        createdBy: undefined,
        createdTime: undefined,
        updatedBy: undefined,
        updatedTime: undefined,
        tenantId: undefined,
        revision: undefined,
        deleted: undefined,
        status: 'Y',
        remark: ''
      }
    },
    // 详情
    handleDetail(row) {
      this.state = 'detail'
      this.visible = true
      // 创建一个新的对象赋值
      this.formData = Object.assign({}, row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 关闭弹出层
    handleClose() {
      this.handleRestFormData()
      this.visible = false
    }
  }
}
</script>
